#pragma once
#include<SDL3/SDL.h>
#include<format>
#define RETURN_SELF return *this;
namespace gfx {
	enum class text_format {
		use_sprintf,
		use_format
	};
	class ascii_text_renderer {
	public:
		ascii_text_renderer(SDL_Renderer* r, text_format f = text_format::use_format): m_r(r), m_f(f) {}
		ascii_text_renderer& text(std::string str, int x, int y) {
			m_x = x;
			m_y = y;
			float scale_x, scale_y;
			SDL_GetRenderScale(m_r, &scale_x, &scale_y);
			SDL_SetRenderScale(m_r, m_size / 8.0f, m_size / 8.0f);
			SDL_RenderDebugText(m_r, x / (scale_x * (m_size / 8.0f)), y / (scale_y * (m_size / 8.0f)), str.data());
			SDL_SetRenderScale(m_r, scale_x, scale_y);
			RETURN_SELF
		}
		template<typename... Arg>
		ascii_text_renderer& textf(std::string f, int x, int y, Arg ...args) {
			m_x = x;
			m_y = y;
			float scale_x, scale_y;
			SDL_GetRenderScale(m_r, &scale_x, &scale_y);
			SDL_SetRenderScale(m_r, m_size / 8.0f, m_size / 8.0f);
			if (m_f == text_format::use_sprintf) {
				SDL_RenderDebugTextFormat(m_r, x / (scale_x * (m_size / 8.0f)), y / (scale_y * (m_size / 8.0f)), f.data(), args...);
			} else {
				SDL_RenderDebugText(m_r, x / (scale_x * (m_size / 8.0f)), y / (scale_y * (m_size / 8.0f)), std::vformat(f, std::make_format_args(args...)).data());
			}
			SDL_SetRenderScale(m_r, scale_x, scale_y);
			RETURN_SELF
		}
		template<typename... Arg>
		ascii_text_renderer& next_linef(std::string f, Arg ...args) {
			textf(f, m_x, m_y + m_size, args...);
			RETURN_SELF
		}
		ascii_text_renderer& next_line(std::string str) {
			text(str.data(), m_x, m_y + m_size);
			RETURN_SELF
		}
		ascii_text_renderer& set_format(text_format f = text_format::use_format){
			m_f=f;
			RETURN_SELF
		}
		ascii_text_renderer& set_text_size(int size){
			m_size=size;
			RETURN_SELF
		}
	private:
		int m_size = 25;
		int m_x = 0, m_y = 0;
		SDL_Renderer* m_r;
		text_format m_f;
	};
}
#undef RETURN_SELF

